package code.example.javassist.maker;

import code.example.javassist.code.MethodCoder;
import javassist.*;

import java.lang.reflect.Method;

public class ClassMarker {
    CtClass ctClass = null;
    public ClassMarker(String className){
        ClassPool classPool =   ClassPool.getDefault();
        CtClass ctClass =  classPool.makeClass(className);
        this.ctClass = ctClass;
    }
    public  void  addInterface(Class interfaceClass) throws Exception {
        ClassPool classPool =   ClassPool.getDefault();
        CtClass interfaceCtClass = classPool.get(interfaceClass.getName());
        ctClass.setInterfaces(new CtClass[]{interfaceCtClass});
    }
    public void  addSuperClass(Class superClass) throws Exception {
        ClassPool classPool =   ClassPool.getDefault();
        CtClass superCtClass = classPool.get(superClass.getName());
        ctClass.setSuperclass(superCtClass);
    }

    public void addMethod(String templateString) throws Exception {
        CtMethod ctMethod = CtNewMethod.make(templateString,ctClass);
        ctClass.addMethod(ctMethod);
    }
    public void addConstructor(String constructorBody) throws Exception {
        CtConstructor ctConstructor = new CtConstructor(new CtClass[]{}, ctClass);
        ctConstructor.setBody(constructorBody);
        ctClass.addConstructor(ctConstructor);
    }
    public Class make() throws Exception {
        return ctClass.toClass();
    }
}
